$(function () {
	//表单提交
    $('#departmentform').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {//表单验证
            code: {
                validators: {
                    notEmpty: {
                        message: '部门编号不能为空'
                    }
                }
            },
            shortName: {
                validators: {
                    notEmpty: {
                        message: '部门简称不能为空'
                    }
                }
            },
            fullName: {
                validators: {
                    notEmpty: {
                        message: '部门名称不能为空'
                    }
                }
            },
            description: {
                validators: {
                    notEmpty: {
                        message: '部门描述不能为空'
                    }
                }
            },
        },
        submitHandler: function (validator, form, submitButton) {
        	var params = $('#departmentform').serialize();
            $.ajax({
                url: ctx + '/department/addDepartement',
                type: 'POST',
                data: params,
                dataType: 'json',
                success: function (data) {
                    if (data.status == "success"){
                        loadHtml('/department/index');
                    }
                    else
                    	toastr.error(data.msg);
                },
                error: function (response) {
                	toastr.error("系统错误");
                }
            });
            return false;
        }
    });
	
    /**
     * 折叠按钮切换
     */
    (function () {
        $('.department .sidebar-menu>li').on('click', '.menu-dropdown', function () {
            if ($(this).next('.submenu').css('display') == 'none') {
                $(this).find('.collapse-right').removeClass('fa-angle-right').addClass('fa-plus-square-o');
                $(this).parent().siblings().find('.collapse-right').removeClass('fa-plus-square-o').addClass('fa-angle-right');
                $(this).parent().siblings().find('.fa-plus-square-o').removeClass('fa-plus-square-o');
            } else {
                $(this).find('.collapse-right').removeClass('fa-plus-square-o').addClass('fa-angle-right');
            }
        });

        $('.department .sidebar-menu li > .submenu > li').on('click', function () {
            if ($(this).find('ul').is('.submenu')) {
                if ($(this).find('.fa-angle-right').hasClass('fa-angle-right')) {
                    $(this).find('.fa-angle-right').removeClass('fa-angle-right').addClass('fa-plus-square-o');
                } else {
                    $(this).find('.fa-plus-square-o').removeClass('fa-plus-square-o').addClass('fa-angle-right');
                }
            } else {
                if (!$(this).find('a').is('.fa-plus-square-o')) {
                    $(this).siblings().find('i:not(".collapse-right")').remove('.fa-plus-square-o');
                    $(this).find('a').append('<i class="fa fa-plus-square-o pull-right"></i>');
                }
            }
        });

        $('.department .sidebar-menu li > .submenu  .submenu>li').on('click', function (event) {
            if (!$(this).find('i').is('.fa-plus-square-o')) {
                $(this).find('a').append('<i class="fa fa-plus-square-o pull-right"></i>');
            }
            event.stopPropagation();
        })
    })();

    /**
     * 部门模块
     */
    (function () {
        //点击部门显示职位名称和部门信息
        $('.department .sidebar-menu .submenu li').on('click', function (event) {
        	var departmentId = $(this).find('.department-id').val();
        	$.ajax({
                url: ctx + '/duty/getAll',
                type: 'POST',
                data: {departmentId : departmentId},
                dataType: 'json',
                success: function (data) {
                	var str = "";
                	for(var i=0; i<data.list.length; i++){
                		str = str + '<li> <a href="javascript:;">'+
                		' <input class="dutyId" type="hidden" value="'+data.list[i].dutyId+'"/>'+
                		' <span class="menu-text">'+data.list[i].fullName+'</span>'+
                		' <input class="code" type="hidden" value="'+data.list[i].code+'"/>'+
                		' <input class="shortName" type="hidden" value="'+data.list[i].shortName+'"/>'+
                		' <input class="departmentName" type="hidden" value="'+data.list[i].departmentName+'"/>'+
                		' <input class="enable" type="hidden" value="'+data.list[i].enable+'"/>'+
                		' <input class="description" type="hidden" value="'+data.list[i].description+'"/>'+
                		' </a> </li>';	
                	}
                	
                	$('#dutyMenu').html(str);
                },
                error: function (response) {
                	toastr.error("系统错误");
                }
            });
        	
        	$('.department-information .departmentId').text($(this).find('.department-id').val());
            $('.department-information .code').text($(this).find('.code').val());//部门编码
            $('.department-information .shortName').text($(this).find('.shortName').val());//部门简称
            $('.department-information .fullName').text($(this).find('.menu-text').eq(0).text());//部门全称-
            if($(this).find('.type').val()==1){
            	$('.department-information .type').text('公司');//部门类型
            }else if($(this).find('.type').val()==2){
            	$('.department-information .type').text('部门');//部门类型
            }else{
            	$('.department-information .type').text('分校');//部门类型
            }
            if($(this).find('.enable').val() == 1){
            	$('.department-information .enable').text('有效');//部门状态
            }else{
            	$('.department-information .enable').text('无效');//部门状态
            }
            $('.department-information .description').text($(this).find('.description').val());//部门描述
            
            if($(this).find('.parent-name').val() == null || $(this).find('.parent-name').val() == '')
            	$('.department-information .parentName').text('无');//父部门名称
            else
            	$('.department-information .parentName').text($(this).find('.parent-name').val());//父部门名称
        	
            $('.addDepartment,.position-information,.addPosition,.editDepartment,.editPosition').hide();
            $('.department-information').show().addClass('animated bounceInRight');
            
        });
        
        //一级菜单信息展示
        $('.department .sidebar-menu .menu-dropdown').on('click', function () {
        	var departmentId = $(this).find('.department-id').val();
        	$.ajax({
                url: ctx + '/duty/getAll',
                type: 'POST',
                data: {departmentId : departmentId},
                dataType: 'json',
                success: function (data) {
                	var str = "";
                	
                	for(var i=0; i<data.list.length; i++){
                		str = str + '<li> <a href="javascript:;">'+
                		' <input class="dutyId" type="hidden" value="'+data.list[i].dutyId+'"/>'+
                		' <span class="menu-text">'+data.list[i].fullName+'</span>'+
                		' <input class="code" type="hidden" value="'+data.list[i].code+'"/>'+
                		' <input class="shortName" type="hidden" value="'+data.list[i].shortName+'"/>'+
                		' <input class="departmentName" type="hidden" value="'+data.list[i].departmentName+'"/>'+
                		' <input class="enable" type="hidden" value="'+data.list[i].enable+'"/>'+
                		' <input class="description" type="hidden" value="'+data.list[i].description+'"/>'+
                		' </a> </li>';	
                	}
                	
                	$('#dutyMenu').html(str);
                },
                error: function (response) {
                	toastr.error("系统错误");
                }
            });
        	$('.department-information .departmentId').text($(this).find('.department-id').val());
            $('.department-information .code').text($(this).find('.code').val());//部门编码
            $('.department-information .shortName').text($(this).find('.shortName').val());//部门简称
            $('.department-information .fullName').text($(this).find('.menu-text').eq(0).text());//部门全称-
            if($(this).find('.type').val()==1){
            	$('.department-information .type').text('公司');//部门类型
            }else if($(this).find('.type').val()==2){
            	$('.department-information .type').text('部门');//部门类型
            }else{
            	$('.department-information .type').text('分校');//部门类型
            }
            if($(this).find('.enable').val() == 1){
            	$('.department-information .enable').text('有效');//部门状态
            }else{
            	$('.department-information .enable').text('无效');//部门状态
            }
            $('.department-information .description').text($(this).find('.description').val());//部门描述
            
            if($(this).find('.parent-name').val() == null || $(this).find('.parent-name').val() == '')
            	$('.department-information .parentName').text('无');//父部门名称
            else
            	$('.department-information .parentName').text($(this).find('.parent-name').val());//父部门名称
        	
            $('.addDepartment,.position-information,.addPosition,.editDepartment,.editPosition').hide();
            $('.department-information').show().addClass('animated bounceInRight');
        	
        })
         //添加部门
	    $('.addDepartment-btn').on('click', function (event) {
	    	$('.addDepartment').find('.parentName').val("无");
	    	console.log($('.addDepartment').find('.parentName').attr('class'));
	    	$('.addDepartment').find('input[name="parentId"]').val("0");
	    	$('.addDepartment').find('input[name="fullPath"]').val("0");
	        $('.department-information,.editDepartment').hide();
	        $('.addDepartment').removeClass('hidden').show();
	        event.stopPropagation();
	    })

        //添加部门
        $('.department a,.departmentTitle,.sidebar-menu').on('click', '.fa-plus-square-o', function (event) {
        	
        	var superiorText = $(this).next('.menu-text').text();
        	$('.addDepartment .parentName').val(superiorText);
        	var departmentId = $(this).nextAll('.department-id').val();
        	var type = $(this).nextAll('.type').val();
        	var parentId = $(this).nextAll('.parentId').val();
        	var companyId = $(this).nextAll('.companyId').val();
        	var parentFullPath = $(this).nextAll('.fullPath').val();
        	var fullPath =[];
        	fullPath.push(parentFullPath);
        	fullPath.push(departmentId);
        	if(type==1||type==3){
        		$('.addDepartment input[name="parentId"]').val(departmentId);
        		$('.addDepartment input[name="companyId"]').val(departmentId);
        	}
        	if(type==2){
        		$('.addDepartment input[name="parentId"]').val(departmentId);
        		$('.addDepartment input[name="companyId"]').val(companyId);
        		
        	}
//            $('.addDepartment input[name="parentId"]').val(departmentId);
            
//            $('.addDepartment input[name="departmentId"]').val(superiorId);
            
            $('.addDepartment input[name="fullPath"]').val(fullPath);
            var superior = $(this).prev('.menu-text').text();
            if (superior.indexOf('(') != -1) {
                superior = superior.substring(0, superior.indexOf('('));
            }
            $('#superior').val(superior);
            $('.department-information,.position-information,.addPosition,.editPosition,.editDepartment').hide();
            $('.addDepartment').removeClass('hidden').show().addClass('animated bounceInRight');
            event.stopPropagation();
            
        });
        
//        $('.departmentTwo').on('click', function (event) {
//        	alert(11);
//        }
        //部门编辑
        $('.department-edit').on('click', function () {
        	$('.editDepartment input[name="departmentId"]').val($('.department-information .departmentId').text());
            $('.editDepartment .code').val($('.department-information .code').text());
            $('.editDepartment .shortName').val($('.department-information .shortName').text());
            $('.editDepartment .fullName').val($('.department-information .fullName').text());
            if($('.department-information .type').text() == '公司'){
            	$('.editDepartment .type').val('1');
            }else if($('.department-information .type').text() == '部门'){
            	$('.editDepartment .type').val('2');
            }else if($('.department-information .type').text() == '分校'){
            	$('.editDepartment .type').val('3');
            }
            $('.editDepartment .parentName').val($('.department-information .parentName').text());

            if ($('.department-information .enable').text() == '有效') {
                $('.editDepartment .valid').prop('checked', true);
            } else {
                $('.editDepartment .invalid').prop('checked', true);
            }
            $('.editDepartment .description').text($('.department-information .description').text());

            $('.department-information,.addDepartment,.position-information,.addPosition,.editPosition').hide();
            $('.editDepartment').removeClass('hidden').show();


            //点击取消按钮
            $('.cancel-btn').on('click', function () {
                $('.editDepartment,.addDepartment,.position-information,.addPosition,.editPosition').hide();
                $('.department-information').removeClass('hidden').show();
            });
        });
    })();

});

$(function () {
    $(".checkAll").click(function () {
        var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
    });

})

//更新部门信息
function updateDepartment(){
	
	console.log($('#updateForm').serialize());
	
	$.ajax({
		url : ctx + '/department/updateByIdDepartement',
		type : 'POST',
		data : $('#updateForm').serialize(),
		dataType : 'json',
		success : function(data){
			if(data.status != "success"){
				toastr.error(data.msg);
			}else{
				loadHtml("/department/index");
			}
		},
		error : function(){
			toastr.error("系统错误");
		}
	});
	return false;
}
//更新职位信息
function updateDuty(){
	$.ajax({
		url : ctx + '/duty/updateRecord',
		type : 'POST',
		data : $('#updateDuty').serialize(),
		dataType : 'json',
		success : function(data){
			if(data.status != "success"){
				toastr.error(data.msg);
			}else{
				loadHtml("/department/index");
			}
		},
		error : function(){
			toastr.error("系统错误");
		}
	});
	return false;
}
//删除职位（假删除）
function deleteDuty(id){
	var dutyId = id;
	$.ajax({
        url: ctx + '/duty/updateRecord',
        type: 'POST',
        data: {
        	dutyId:dutyId,
        	deleteMark:0
        },
        dataType: 'json',
        success: function (data) {
        	if(data.status == 'success'){
        		dutyDataTable.init();
        	}else{
        		toastr.error(data.msg);
        	}
        }
	});
}
